package com.lening.provider.controller.OwnersDecoration;

import com.lening.common.base.BaseController;
import com.lening.provider.dto.OwnersDecoration.DecorationDrawingsDTO;
import com.lening.provider.po.OwnersDecoration.DecorationDrawingsPO;
import com.lening.provider.service.OwnersDecoration.DecorationDrawingsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 刘政华
 * @datetime 2024/6/5 11:46
 * @description 类对象
 */
@Slf4j
@RestController
@RequestMapping("decorationDrawingss")
public class DecorationDrawingsController extends BaseController<DecorationDrawingsPO, DecorationDrawingsDTO, DecorationDrawingsService> {

    @Value("${upload.imagePath}")
    private String uploadPath;

    @Resource
    private DecorationDrawingsService decorationDrawingsService;

    @PostMapping("/upload")
    public String uploadImages(@RequestParam("files") MultipartFile[] files) {
        List<String> fileNames = new ArrayList<>();
        try {
            for (MultipartFile file : files) {
                // 获取文件名
                String fileName = file.getOriginalFilename();
                if (fileName != null && !fileName.isEmpty()) {
                    // 指定保存路径
                    String drawing = uploadPath + fileName;
                    // 保存文件到本地
                    file.transferTo(new File(drawing));
                    fileNames.add(drawing); // 可以收集文件名用于后续处理或返回

                    // 创建DecorationDrawingsPO对象并存储到数据库（这里假设每个文件都创建一个记录）
                    DecorationDrawingsPO decorationDrawingsPO = new DecorationDrawingsPO();
                    decorationDrawingsPO.setDrawing(drawing);
                    decorationDrawingsService.add(decorationDrawingsPO);
                }
            }
            return "批量上传成功";
        } catch (IOException e) {
            e.printStackTrace();
            return "批量上传失败";
        }
    }

}
